﻿#include "EditorStatusBarView.h"
#include "EditorStatusBarViewModel.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "StatusBar/EditorStatusItemViewModel.h"
#include "StatusBar/EditorStatusItemView.h"
#include "Framework/Editor.h"
void REditorStatusBarView::BindViewModel(TSharedObjectPtr<REditorStatusBarViewModel> InModel)
{
    BaseViewModel=InModel;

    //创建状态项视图
    for (int i = 0; i < BaseViewModel->Items.Num(); ++i)
    {
        TSharedObjectPtr<REditorStatusItemView> ItemView=NewObject<REditorStatusItemView>(
            REditor::Get()->GetViewClass(BaseViewModel->Items[i]->GetClass())
            ,this);
        ItemView->BindViewModel(BaseViewModel->Items[i]);
        Items.Add(ItemView);
    }

}

void REditorStatusBarView::FillWidgets(TSharedObjectPtr<RWidget> InParentWidget)
{
    //状态栏项分左右两部分
    //左边的左对齐,右边的右对齐
    //当宽度不够时隐藏右边的最左侧的项

    //左部分,宽度就是内容的宽度
    RHorizontalPositionerWidgetPtr LeftPart=NewWidget<RHorizontalPositionerWidget>(InParentWidget);
    LeftPart->Anchor.VerticalFill(InParentWidget);
    RHorizontalPositionerWidgetWeakPtr WeakLeft=LeftPart;
    LeftPart->Width=[WeakLeft]()->float {
        return WeakLeft->CalculateContentWidth();
    };

    //右部分，宽度是状态栏剩余宽度
    RHorizontalPositionerWidgetPtr RightPart=NewWidget<RHorizontalPositionerWidget>(InParentWidget);
    RightPart->Anchor.VerticalFill(InParentWidget);
    RightPart->Anchor.LeftReference=LeftPart;
    RightPart->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    RightPart->Anchor.RightReference=InParentWidget;
    RightPart->Align=EHorizontalPositionerAlign::Right;

    float Spaceing=10.0f;
    LeftPart->Spacing=Spaceing;
    RightPart->Spacing=Spaceing;

    for(auto Item:Items)
    {
        Item->FillWidgets(LeftPart,RightPart);
    }
}
